package ma.mindware.mpm.domain.account;

import java.sql.ResultSet;
import java.sql.SQLException;

import jpmapper.core.Transaction;
import jpmapper.exceptions.JPMapperException;
import ma.mindware.met.core.account.User;
import ma.mindware.met.core.technicalservice.DBService;
import ma.mindware.met.core.technicalservice.TechnicalServiceException;

public class AccountDBService extends DBService{


	public AccountDBService(Object[] parameters)
			throws TechnicalServiceException {
		super(parameters);
		// TODO Auto-generated constructor stub
	}
	
	
	public String getUserRole(User u) throws TechnicalServiceException{
		
        Transaction transaction;
        try
        {
            transaction = jpmapperFacade.getNewTransaction();
            transaction.begin();
            String query = "SELECT role.name FROM role, userrole WHERE userrole.accountID = "+u.getAccountID()+" AND userrole.roleID = role.roleID";
//            User user = (User)transaction.buildObject(transaction.executeQuery(query), new User());
            ResultSet role=transaction.executeQuery(query);
            role.first();
            transaction.end();
            return role.getString(1);
        }
        catch(JPMapperException e)
        {
            throw new TechnicalServiceException(0, e.getErrorMessage(), e);
        } catch (SQLException e) {
			// TODO Auto-generated catch block
        	System.out.println("AccountDBService.getUserRole()");
			e.printStackTrace();
		}
  
		return "";
	}
	
	
	

}
